//#define __DEBUG__
// by Xeno
#define THIS_FILE "fn_getsidemission.sqf"
#include "x_setup.sqf"
private ["_ffolder", "_mfolder"];
if (!isServer) exitWith{};

if (__XJIPGetVar(all_sm_res)) exitWith {};

if (GVAR(current_mission_counter) == GVAR(number_side_missions)) exitWith {
	["all_sm_res",true] call FUNC(NetSetJIP);
	["all_sm_res"] call FUNC(NetCallEvent);
	#ifndef __TT__
	GVAR(kb_logic1) kbTell [GVAR(kb_logic2),GVAR(kb_topic_side),"AllSMissionsResolved","SIDE"];
	#else
	GVAR(hq_logic_en1) kbTell [GVAR(hq_logic_en2),"HQ_W","AllSMissionsResolved","SIDE"];
	GVAR(hq_logic_ru1) kbTell [GVAR(hq_logic_ru2),"HQ_E","AllSMissionsResolved","SIDE"];
	#endif
};

if (GVAR(MissionType) != 2) then {
	while {!GVAR(main_target_ready)} do {sleep 2.321};
};

_cur_sm_idx = GVAR(side_missions_random) select GVAR(current_mission_counter);
__INC(GVAR(current_mission_counter));

GVAR(x_sm_rem_ar) = [];
GVAR(x_sm_vec_rem_ar) = [];

//_cur_sm_idx = _this select 0;
//_cur_sm_idx = 50;

_ffolder = "ma3";

_mfolder = "x_missions\" + _ffolder + "\%2%1.sqf";

if (isNil QGVAR(HC_CLIENT_OBJ)) then {
	__TRACE("No HC Client")
	execVM (format [_mfolder, _cur_sm_idx, GVAR(sm_fname)]);
} else {
	__TRACE_1("",QGVAR(HC_CLIENT_OBJ))
	[QGVAR(hcexecsm), [GVAR(HC_CLIENT_OBJ), format [_mfolder, _cur_sm_idx, GVAR(sm_fname)]]] call FUNC(NetCallEventSTO);
};

sleep 7.012;
[QGVAR(cur_sm_idx),_cur_sm_idx] call FUNC(NetSetJIP);
[QGVAR(up_m)] call FUNC(NetCallEventToClients);
#ifndef __TT__
GVAR(kb_logic1) kbTell [GVAR(kb_logic2),GVAR(kb_topic_side),"NewMission","SIDE"];
#else
GVAR(hq_logic_en1) kbTell [GVAR(hq_logic_en2),"HQ_W","NewMission","SIDE"];
GVAR(hq_logic_ru1) kbTell [GVAR(hq_logic_ru2),"HQ_E","NewMission","SIDE"];
#endif
GVAR(sm_resolved) = false;
GVAR(sm_winner) = 0;
